﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IncoolReg
{
    public partial class UploadForm : Form
    {
        public UploadForm()
        {
            InitializeComponent();
        }

        private void UploadForm_Load(object sender, EventArgs e)
        {
            listViewRegMsg.BeginUpdate();
            List<Hashtable> list = OfflineUtility.GetOfflineData();
            foreach (Hashtable ht in list)
            {
                var item =  listViewRegMsg.Items.Add(new ListViewItem()
                {
                    Tag =  ht,
                    Text = ht["equipment"].ToString()
                } );
                item.SubItems.Add(ht["hospitail_name"].ToString());
                item.SubItems.Add(ht["office_name"].ToString());
                item.SubItems.Add("未上传");
            }
            listViewRegMsg.EndUpdate();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            this.Cursor = Cursors.WaitCursor;
            #if DEBUG
            Thread.Sleep(1000);
            #endif 
            foreach (ListViewItem item in listViewRegMsg.Items)
            {
                Hashtable ht = item.Tag as Hashtable;
                string str = ht["filepath"].ToString().Trim();
                string errormsg = string.Empty;
                if (OfflineUtility.SaveEquipmentData(ht, out errormsg))
                {
                    if (File.Exists(str + ".bak")) File.Delete(str + ".bak");
                    File.Copy(str, str + ".bak");
                    File.Delete(str);
                    listViewRegMsg.BeginUpdate();
                    item.SubItems[3].Text = "上传成功";
                    listViewRegMsg.EndUpdate();
                }
                else
                {
                    listViewRegMsg.BeginUpdate();
                    item.SubItems[3].Text = errormsg;
                    listViewRegMsg.EndUpdate();
                    MessageBox.Show(string.Format("名称为{0}的数据因{1}的原因上传失败！",ht["equipment"],errormsg));
                    
                }

            }
            this.Cursor = Cursors.Default;
            this.Hide();
            OnlineForm form = new OnlineForm();
            form.Show();
        }
    }
}
